﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cesave.Domain.Core;

namespace Cesave.Domain.PermisosModule.Contracts.Acreditaciones
{
    public class AcreditacionService
        : IAcreditacionService
      
    {
        private IAcreditacionRepository _acreditacionrepository;

        public AcreditacionService(IAcreditacionRepository acreditacionrepository)
        {
            _acreditacionrepository = acreditacionrepository;
        }

        public void AddAcreditacion(Entities.Acreditacion acreditacion)
        {
            IUnitOfWork unitofwork = _acreditacionrepository.StoreContext;
            _acreditacionrepository.Add(acreditacion);
            try
            {
                unitofwork.Commit();
            }
            catch (Exception)
            {
                unitofwork.RollbackChanges();
                unitofwork.CommitAndRefreshChanges();
            }
        }

        public void ChangeAcreditacion(Entities.Acreditacion acreditacion)
        {
            IUnitOfWork unitofwork = _acreditacionrepository.StoreContext;
            _acreditacionrepository.Modify(acreditacion);
            try
            {
                unitofwork.Commit();
            }
            catch (Exception)
            {
                unitofwork.RollbackChanges();
                unitofwork.CommitAndRefreshChanges();
            }
        }

        public Entities.Acreditacion FindAcreditacionByGuid(Guid acreditacionGuid)
        {
             AcreditacionByGuidSpecification specification = new AcreditacionByGuidSpecification (acreditacionGuid);
            return _acreditacionrepository.FindAcreditacionByGuidSpecification(specification);
        }

        public void Dispose()
        {
            if (_acreditacionrepository != null && _acreditacionrepository.StoreContext != null)
                _acreditacionrepository.StoreContext.Dispose();
        }
    }
}
